//
//  IAASettingVC.m
//  iPadAuditAuthorise
//
//  Created by XiangDongMBP on 16/9/5.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "IAASettingVC.h"
#import "GlobalHeader.h"
#import "IAAModifyPwdVC.h"

@interface IAASettingVC ()<UITableViewDelegate, UITableViewDataSource>

@property (weak,   nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *dataArray;

@end

static NSString * const reuseCellId = @"UITableViewCell";
static NSString * const reuseLogOutCellId = @"reuseLogOutCellId";

@implementation IAASettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initLogicData];
    [self initNavigationBar];
    [self initSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initLogicData {
    NSString *origId = [NSString stringWithFormat:@"机构号：%@", IAA_APP_DELEGATE.organizeId];
    NSString *userId = [NSString stringWithFormat:@"柜员号：%@", IAA_APP_DELEGATE.userNo];
    self.dataArray = @[@[origId, userId, @"setting_user"],
                       @[@"修改密码", @"", @"setting_pwd"],
                       @[@"退出登录", @"", @""]];
}

- (void)initNavigationBar {
    self.navigationItem.title = @"设置";
}

- (void)initSubViews {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

#pragma mark - <UITableViewDelegate>代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (indexPath.section == 0) ? 90 : 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.dataArray[indexPath.section][0] isEqualToString:@"修改密码"]) {
        [self modifyPwd];
    } else if ([self.dataArray[indexPath.section][0] isEqualToString:@"退出登录"]) {
        [self requestlogut];
    }
}

#pragma mark - <UITableViewDataSource>代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != self.dataArray.count - 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseCellId];
        }
        cell.textLabel.text = self.dataArray[indexPath.section][0];
        cell.detailTextLabel.text = self.dataArray[indexPath.section][1];
        cell.imageView.image = [UIImage imageNamed:self.dataArray[indexPath.section][2]];
        cell.accessoryType = (indexPath.section != 0) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
        cell.backgroundColor = IAA_COLOR_0xF7F7F7(1.0);
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseLogOutCellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseLogOutCellId];
        }
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = IAA_COLOR_0xFE6064(1.0);
        cell.textLabel.text = self.dataArray[indexPath.section][0];
        cell.backgroundColor = IAA_COLOR_0xF7F7F7(1.0);
        return cell;
    }
}

#pragma mark - 按键处理
- (void)requestlogut {
    NSString *urlStr = [IAAUrlProtocolMerge appLoginOutAction];
    
    NSDictionary *pars = @{@"organizeId"  : IAA_APP_DELEGATE.organizeId,
                           @"uname" : IAA_APP_DELEGATE.userNo,
                           @"token" : IAA_APP_DELEGATE.token,
                           @"osName": @"iOS"};
    
    // 顯示等待圈圈
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [XDAFHttpRequest POST:urlStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"登出请求返回%@", responseObject);
        [hud hide:NO];
        if ([responseObject[@"resultCode"] isEqualToString:IAA_RESULT_CODE_SUC] ||
            [responseObject[@"resultCode"] isEqualToString:@"0007"]) {
            [self jumpToLoginVC];
        } else {
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:responseObject[@"resultMsg"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [hud hide:NO];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
    }];
}

- (void)jumpToLoginVC {
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController * vc = [mainSB instantiateViewControllerWithIdentifier:@"IAALoginVC"];
    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
    //转场动画
    CATransition *anim = [CATransition animation];
    //    anim.type = @"rippleEffect";
    anim.duration = 0.3;
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
}

- (void)modifyPwd{
//    // 校验输入参数
//    if (!self.pwdOldTextField.text.length) {
//        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请输入原密码"];
//        return;
//    }
//    if (!self.pwdNewTextField.text.length) {
//        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请输入新密码"];
//        return;
//    }
//    // 顯示等待圈圈
//    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    NSString *urlStr = [IAAUrlProtocolMerge appModifyPwd];
//    NSString *userNo = [[NSUserDefaults standardUserDefaults] objectForKey:IAA_USERDEF_UNAME];
//    NSDictionary *pars = @{@"userNo"  : userNo,
//                           @"token" : IAA_APP_DELEGATE.token,
//                           @"opassWord" : self.pwdOldTextField.text,
//                           @"npassWord": self.pwdNewTextField.text};
//    
//    [XDAFHttpRequest POST:urlStr parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [hud hide:NO];
//        if ([responseObject[@"resultCode"] isEqualToString:IAA_RESULT_CODE_SUC]) {
//            [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"修改密码成功"];
//        } else {
//            [MBProgressHUD showPromptWithAutoHideInView:self.view message:responseObject[@"resultMsg"]];
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [hud hide:NO];
//        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
//    }];
    
    IAAModifyPwdVC *vc = [[IAAModifyPwdVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - <UIImagePickerControllerDelegate>代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//    UIImage *image = info[UIImagePickerControllerOriginalImage];
//    self.bigImgView.image = image;
//    NSData *data = UIImageJPEGRepresentation(image, IAA_JPEG_QUALITY);
//    NSString *base64 = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//    if (self.curIndex < self.model.imgelist.count) {
//        IAAAuthCheckImgItem *item = self.model.imgelist[self.curIndex];
//        item.isReplace = YES;
//        item.imageBase64 = base64;
//    } else {
//        IAAAuthCheckImgItem *item = [[IAAAuthCheckImgItem alloc] init];
//        item.imageBase64 = base64;
//        item.isReTakePicture = YES; // 允许再次补拍
//        item.isReTake = YES;        // 标记是新增的
//        item.ufid = [NSString stringWithFormat:@"%zd", self.curIndex];
//        [self.model.imgelist addObject:item];
//    }
    [self dismissViewControllerAnimated:YES completion:nil];
//    [self refreshWholeView];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
